package com.example.smartall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.entity.OrderBean;
import com.example.smartall.ui.OrderDetailsActivity;

import java.util.List;
import java.util.logging.Handler;

public class OrderAdapter extends BaseAdapter {

    private Context context;
    private List<OrderBean.RowsBean> data;

    public OrderAdapter(Context context, List<OrderBean.RowsBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null){
            viewHolder = new ViewHolder();
            view  = LayoutInflater.from(context).inflate(R.layout.common_list_item,null,false);
            viewHolder.img = (ImageView) view.findViewById(R.id.img);
            viewHolder.title = (TextView) view.findViewById(R.id.title);
            viewHolder.subtitle = (TextView) view.findViewById(R.id.subtitle);
            viewHolder.content = (TextView) view.findViewById(R.id.content);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(ApiConfig.BASE_URL + data.get(i).getImgUrl()).into(viewHolder.img);
        viewHolder.title.setText(data.get(i).getName());
        viewHolder.subtitle.setText(data.get(i).getAddress());
        viewHolder.content.setText(data.get(i).getIntroduction());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, OrderDetailsActivity.class);
                intent.putExtra("orderName",data.get(i).getName());
                intent.putExtra("orderId",data.get(i).getId());
                context.startActivity(intent);
            }
        });
        return view;
    }

    static class ViewHolder{
        private ImageView img;
        private TextView title;
        private TextView subtitle;
        private TextView content;
    }
}
